﻿using ECANPort;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsingEventAggregator.Core;
using HCC = HandyControl.Controls;

namespace TLDNs.ViewModels
{
    public class CANInitConfigWindowViewModel : EAViewModelBase
    {
        public CANInitConfigWindowViewModel(IContainerExtension ce, IRegionManager rm, IEventAggregator ea) : base(ce, rm, ea)
        {
            _ea.GetEvent<InitCan1ConfigEvent>().Subscribe(InitCan1ConfigEvent_Sub);
            _ea.GetEvent<InitCan2ConfigEvent>().Subscribe(InitCan2ConfigEvent_Sub);

            BaudDataList = new ObservableCollection<string>();
            BaudDataList.AddRange(Enum.GetNames(typeof(CanBaud)));

            ModeDataList = new ObservableCollection<string>();
            ModeDataList.AddRange(Enum.GetNames(typeof(CanMode)));
            FilterDataList = new ObservableCollection<byte>();
            FilterDataList.Add(0);
            FilterDataList.Add(1);

            WinClosingEvent = new DelegateCommand(WinClosingEvent_Sub);
            ApplyCan1ConfigCommand = new DelegateCommand(ApplyCan1ConfigCommand_Sub);
            ApplyCan2ConfigCommand = new DelegateCommand(ApplyCan2ConfigCommand_Sub);
        }

        private void ApplyCan1ConfigCommand_Sub()
        {
            _ea.GetEvent<InitCan1ConfigOkEvent>().Publish((Can1, Can1Enable));
            HCC.Growl.Info($"{LangDic["ConfigApply"]}");
           
        }

        private void ApplyCan2ConfigCommand_Sub()
        {
            _ea.GetEvent<InitCan2ConfigOkEvent>().Publish((Can2, Can2Enable));
            HCC.Growl.Info($"{LangDic["ConfigApply"]}");
        }


        public DelegateCommand WinClosingEvent { get; private set; }
        public DelegateCommand ApplyCan1ConfigCommand { get; private set; }
        public DelegateCommand ApplyCan2ConfigCommand { get; private set; }

        private void WinClosingEvent_Sub()
        {

        }

        private void InitCan1ConfigEvent_Sub((object obj, bool Enable) tag)
        {
            Can1 = (INIT_CONFIG)tag.obj;
            Can1Enable = tag.Enable;
            var rt = INIT_CONFIG.GetCanBaud(Can1.Timing0, Can1.Timing1);
            Can1Baud = rt.baud;
            Can1AccCode = Can1.AccCode;
            Can1AccCodeHex = Can1AccCode.ToString("X");
            Can1AccMask = Can1.AccMask;
            Can1AccMaskHex = Can1AccMask.ToString("X");
            Can1Filter = Can1.Filter;
            Can1Mode = (CanMode)Can1.Mode;
        }
        private void InitCan2ConfigEvent_Sub((object obj, bool Enable) tag)
        {
            Can2 = (INIT_CONFIG)tag.obj;
            Can2Enable = tag.Enable;
            var rt = INIT_CONFIG.GetCanBaud(Can2.Timing0, Can2.Timing1);
            Can2Baud = rt.baud;
            Can2AccCode = Can2.AccCode;
            Can2AccCodeHex = Can2AccCode.ToString("X");
            Can2AccMask = Can2.AccMask;
            Can2AccMaskHex = Can2AccMask.ToString("X");
            Can2Filter = Can2.Filter;
            Can2Mode = (CanMode)Can2.Mode;
        }

        private bool _Can1Enable;
        public bool Can1Enable
        {
            get { return _Can1Enable; }
            set { SetProperty(ref _Can1Enable, value); }
        }
        private bool _Can2Enable;
        public bool Can2Enable
        {
            get { return _Can2Enable; }
            set { SetProperty(ref _Can2Enable, value); }
        }


        public INIT_CONFIG Can1;
        public INIT_CONFIG Can2;

        private string _Can1AccCodeHex;
        public string Can1AccCodeHex
        {
            get { return _Can1AccCodeHex; }
            set
            {
                uint tmp;
                if (PubMod.HexStringToUint(value, out tmp))
                {
                    SetProperty(ref _Can1AccCodeHex, value.ToUpper()) ;
                    Can1AccCode = tmp;
                }
            }
        }


        private uint _Can1AccCode;
        public uint Can1AccCode
        {
            get { return _Can1AccCode; }
            set
            {
                SetProperty(ref _Can1AccCode, value);
                Can1.AccCode = value;
                Can1AccCodeBin = PubMod.ValueToBinString(value);
            }
        }


        private string _Can1AccCodeBin;
        public string Can1AccCodeBin
        {
            get { return _Can1AccCodeBin; }
            set
            {
                SetProperty(ref _Can1AccCodeBin, value);
            }
        }


        private string _Can1AccMaskBin;
        public string Can1AccMaskBin
        {
            get { return _Can1AccMaskBin; }
            set
            {
                SetProperty(ref _Can1AccMaskBin, value);
            }
        }

        private string _Can1AccMaskHex;
        public string Can1AccMaskHex
        {
            get { return _Can1AccMaskHex; }
            set
            {
                uint tmp;
                if (PubMod.HexStringToUint(value, out tmp))
                {
                    SetProperty(ref _Can1AccMaskHex, value.ToUpper());
                    Can1AccMask = tmp;
                }
            }
        }



        private uint _Can1AccMask;
        public uint Can1AccMask
        {
            get { return _Can1AccMask; }
            set
            {
                SetProperty(ref _Can1AccMask, value);
                Can1.AccMask = value;
                Can1AccMaskBin = PubMod.ValueToBinString(value);
            }
        }

        private byte _Can1Filter;
        public byte Can1Filter
        {
            get { return _Can1Filter; }
            set
            {
                if (value > 1)
                    value = 1;
                SetProperty(ref _Can1Filter, value);
                Can1.Filter = value;
            }
        }

        private CanMode _Can1Mode;
        public CanMode Can1Mode
        {
            get { return _Can1Mode; }
            set
            {
                SetProperty(ref _Can1Mode, value);
                Can1.Mode = (byte)value;
            }
        }


        private CanBaud _Can1Baud = CanBaud.B_500K;
        public CanBaud Can1Baud
        {
            get { return _Can1Baud; }
            set
            {
                SetProperty(ref _Can1Baud, value);
                INIT_CONFIG.SetBaudrate(value, ref Can1);
            }
        }



        private string _Can2AccCodeHex;
        public string Can2AccCodeHex
        {
            get { return _Can2AccCodeHex; }
            set
            {
                uint tmp;
                if (PubMod.HexStringToUint(value, out tmp))
                {
                    SetProperty(ref _Can2AccCodeHex, value.ToUpper());
                    Can2AccCode = tmp;
                }
            }
        }


        private uint _Can2AccCode;
        public uint Can2AccCode
        {
            get { return _Can2AccCode; }
            set
            {
                SetProperty(ref _Can2AccCode, value);
                Can2.AccCode = value;
                Can2AccCodeBin = PubMod.ValueToBinString(value);
            }
        }


        private string _Can2AccCodeBin;
        public string Can2AccCodeBin
        {
            get { return _Can2AccCodeBin; }
            set
            {
                SetProperty(ref _Can2AccCodeBin, value);
            }
        }


        private string _Can2AccMaskBin;
        public string Can2AccMaskBin
        {
            get { return _Can2AccMaskBin; }
            set
            {
                SetProperty(ref _Can2AccMaskBin, value);
            }
        }

        private string _Can2AccMaskHex;
        public string Can2AccMaskHex
        {
            get { return _Can2AccMaskHex; }
            set
            {
                uint tmp;
                if (PubMod.HexStringToUint(value, out tmp))
                {
                    SetProperty(ref _Can2AccMaskHex, value.ToUpper());
                    Can2AccMask = tmp;
                }
            }
        }


        private uint _Can2AccMask;
        public uint Can2AccMask
        {
            get { return _Can2AccMask; }
            set
            {
                SetProperty(ref _Can2AccMask, value);
                Can2.AccMask = value;
                Can2AccMaskBin = PubMod.ValueToBinString(value);
            }
        }

        private byte _Can2Filter;
        public byte Can2Filter
        {
            get { return _Can2Filter; }
            set
            {
                if (value > 1)
                    value = 1;
                SetProperty(ref _Can2Filter, value);
                Can2.Filter = value;
            }
        }

        private CanMode _Can2Mode;
        public CanMode Can2Mode
        {
            get { return _Can2Mode; }
            set
            {
                SetProperty(ref _Can2Mode, value);
                Can2.Mode = (byte)value;
            }
        }


        private CanBaud _Can2Baud = CanBaud.B_500K;
        public CanBaud Can2Baud
        {
            get { return _Can2Baud; }
            set
            {
                SetProperty(ref _Can2Baud, value);
                INIT_CONFIG.SetBaudrate(value, ref Can2);
            }
        }



        public ObservableCollection<string> _BaudDataList;
        public ObservableCollection<string> BaudDataList
        {
            get { return _BaudDataList; }
            set { SetProperty(ref _BaudDataList, value); }
        }


        public ObservableCollection<string> _ModelDataList;
        public ObservableCollection<string> ModeDataList
        {
            get { return _ModelDataList; }
            set { SetProperty(ref _ModelDataList, value); }
        }

        public ObservableCollection<byte> _FilterDataList;
        public ObservableCollection<byte> FilterDataList
        {
            get { return _FilterDataList; }
            set { SetProperty(ref _FilterDataList, value); }
        }


        protected override void SFMFileIsReLoaded()
        {

        }
    }
}
